package cn.tedu.store.service;

import javax.annotation.Resource;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.tedu.store.bean.User;
import cn.tedu.store.mapper.UserMapper;
import cn.tedu.store.service.ex.PassordNotMatchException;
import cn.tedu.store.service.ex.UsernameAlreadyExistsException;
import cn.tedu.store.service.ex.UsernameNotFoundException;

@Service("userService")
public class UserServiceImpl implements IUserService{
	@Resource(name="userMapper")
	private UserMapper userMapper;
	@Value("#{dbConfig.salt}")
	String salt;
	public Integer register(User user) {
		boolean flag=checkUsernameExists(user.getUsername());
		if(flag==true) {
			throw new UsernameAlreadyExistsException("用户已存在");
			
		}else {
			//对用户密码进行加密处理
			//获取密码明文
			String pwd=user.getPassword();
			
			//加盐摘要，计算密码的密文
			//String salt="今天你吃了吗？";
			String md5=DigestUtils.md5Hex(pwd+salt);
			//保存密文到数据库中
			user.setPassword(md5);
			userMapper.insert(user);
			return user.getId();
			
		}
		
	}

	public User UserFindUserByUsername(String username) {
		
		return userMapper.findUserByUsername(username);
	}

	public boolean checkEmailExists(String email) {
		
		return userMapper.getRecordCountByEmail(email)==1;
	}

	public boolean checkPhoneExists(String phone) {
		
		return userMapper.getRecordCountByPhone(phone)==1;
	}

	public boolean checkUsernameExists(String username) {
		return userMapper.findUserByUsername(username)!=null;
	}

	public User login(String username, String password) {
		User u=UserFindUserByUsername(username);
		if(u!=null) {
			String md5=DigestUtils.md5Hex(password+salt);
			if(u.getPassword().equals(md5)) {
				//登录成功
				return u;
			}else {
				throw new PassordNotMatchException("密码错误");
			}
		}else {
			throw new UsernameNotFoundException("用户不存在");
		}
	}

	public Integer changePassword(Integer id, String oldPassword, String newPassword) {
		User user=userMapper.findUserById(id);
		if(user==null) {
			throw new UsernameNotFoundException("用户不存在");
		}else {
			if(!(oldPassword.equals(user.getPassword()))) {
				throw new PassordNotMatchException("密码错误");
			}else {
				User newUser=new User();
				newUser.setPassword(newPassword);
				newUser.setId(id);
				userMapper.update(newUser);
			}
		}
		return null;
	}

	public Integer editProfile(Integer id, String username, Integer gender, String phone, String email) {
		User user=new User();
		user.setId(id);
		User u=userMapper.findUserById(id);
		//1.封装用户名
		if(u==null) {
			throw new UsernameNotFoundException("用户不存在");
		}
		if(u.getUsername().equals(username)) {
			
		}else {
			User u2=userMapper.findUserByUsername(username);
			if(u2==null) {
				user.setUsername(username);
			}else {			
					throw new UsernameAlreadyExistsException("用户名已经被占用");				
			}
		}
		//2.封装性别
		user.setGender(gender);
		//3.封装电话
		if(phone!=null&&phone.length()>=11) {
		user.setPhone(phone);
		}
		//4.封装邮箱
		if(email!=null&&!"".equals(email)) {
		user.setEmail(email);
		}
		//5.执行更新并返回相关数据
		return userMapper.update(user);
	}

	public User findUserById(Integer id) {
		
		return userMapper.findUserById(id);
	}
	
	
}
